package com.uchengwang.functions.http.manager;

import android.app.Activity;

import com.uchengwang.functions.http.core.HttpCore;
import com.uchengwang.functions.http.listener.IBaseRequest;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.HttpCallBack;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by U城网 on 2017/3/2.
 */

public class HttpManager implements IHttpManager {
    //可以表单提交 可以json提交 以后需要用到表单提交的，再写方法，将相关字段改为true就行

    //在两个或者更多的线程访问的成员变量上使用volatile
//    private static volatile HttpCore httpCore;
    private HttpCore httpCore;

    private static HttpManager manager;

    private HttpManager(Activity activity) {
        if (httpCore == null) {
            httpCore = new HttpCore(activity);
        }
    }


//    public static HttpManager newInstance(Activity activity) {
//        if (manager == null || httpCore == null){
//            manager = new HttpManager(activity);
//        }
//        return manager;
//    }

    public static HttpManager newInstance(Activity activity) {
        if (manager == null){
            manager = new HttpManager(activity);
        }
        return manager;
    }

    public void setNull(){
        manager = null;
        httpCore = null;
    }
    /**
     *  异步请求
     * */
    @Override
    public Call query(RequestMethod method, String url, IBaseRequest request, HttpCallBack callBack) {
        httpCore.setAnd(false);
        return httpCore.query(method,false, url, request, callBack);
    }

    /**
     * 异步请求 针对get delete 拼接好的参数转化为String
     * */
    public Call queryString(RequestMethod method, String url, String request, HttpCallBack callBack){
        return httpCore.queryString(method, url, request, callBack);
    }

    /**
     * 版本升级控制 ?key=value拼接 一个键值对
     * */
    public Call queryCheck(RequestMethod method, String url, IBaseRequest request, HttpCallBack callBack) {
        httpCore.setAnd(true);
        return httpCore.query(method,false, url, request, callBack);
    }

    /**
     * 同步请求
     * */
    @Override
    public Response synQuery(RequestMethod method, String url, IBaseRequest request) throws IOException {
        return httpCore.synQuery(method,false, url, request);
    }

    /**
     * 调用非U城的接口 请求数据
     * */
    public Call queryOther(RequestMethod method, String url, IBaseRequest request, HttpCallBack callBack){
        return httpCore.queryOther(method,false, url, request, callBack);
    }
}
